<template>
  <el-container>
    <!-- 头像区域 -->
    <el-header>
      <div class="logo">
        <img src="../assets/logo.jpg" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 菜单区域 -->
    <el-container>
      <el-aside :width="collapse ? 64+'px' : 200+'px'">
        <div class="contract" @click="shrink">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#4090cb"
          unique-opened
          :collapse="collapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="Menuone.id+''" v-for="Menuone in Menuones" :key="Menuone.id">
            <template slot="title">
              <i :class="iconsObj[Menuone.id]"></i>
              <span>{{Menuone.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+Menutwo.path" 
            v-for="Menutwo in Menuone.children" 
            :key="Menutwo.id"
            @click="saveNavState('/'+Menutwo.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{Menutwo.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      Menuones:[],//一级菜单名字
      collapse:false,//是否折叠
      //一级菜单图标
      iconsObj:{
        125:'iconfont icon-users',
        103:'iconfont icon-tijikongjian',
        101:'iconfont icon-shangpin',
        102:'iconfont icon-danju',
        145:'iconfont icon-baobiao'
      },
      activePath:''//激活路径
    };
  },

  created() {
    this.getMenuList()//菜单渲染
    this.activePath=window.sessionStorage.getItem('activePath')//取出sessionStorage中保持激活的路径
  },

  methods: {
    //退出
    logout() {
      this.$router.push("/login");
      window.sessionStorage.clear();
    },

    async getMenuList(){
     const {data:res}= await this.$http.get('menus')
        // console.log(res);
        this.Menuones=res.data  
    },
    //是否折叠
    shrink(){
      this.collapse=!this.collapse
    },
    // 把激活的路径保存到sessionStorage中
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath=activePath
      
    }

  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}

.el-menu{
  border-right: 0;
}
.el-header {
  background-color: #373d3f;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  img {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    margin-right: 20px;
  }
  .logo {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 20px;
  }
}

.el-aside {
  background-color: #333744;
  .contract{
    text-align: center;
    line-height: 24px;
    color: #fff;
    font-size: 10px;
    letter-spacing: .2em;
    background-color:#4a5064;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont{
  margin-right: 10px;
}
</style>